var app = getApp();
var Model = {
  ip: app.globalData.API_URL,
  prot: '',
  //上传图片
  uploadImg: function(option) {
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        wx.showLoading({
          title: '正在上传...',
          mask: false
        })
        wx.uploadFile({
          url: app.globalData.report + '/blade-resource/oss/endpoint/put-file',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            "content-type": "application/json;charset=UTF-8",
            "Blade-Auth": app.globalData.authInfo.token_type + " " + app.globalData.authInfo.access_token
          },
          success(ret) {
            wx.hideLoading()
            const data = ret.data
            option.success(data, tempFilePaths)
          }
        })

      }
    })
  },
  //上传图片Ocr
  uploadImgOcr: function (option) {
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        wx.showLoading({
          title: '正在上传...',
          mask: false
        })
        wx.uploadFile({
          url: app.globalData.report + '/blade-resource/oss/endpoint/put-file-ocr',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            "content-type": "application/json;charset=UTF-8",
            "Blade-Auth": app.globalData.authInfo.token_type + " " + app.globalData.authInfo.access_token
          },
          success(ret) {
            wx.hideLoading()
            const data = ret.data
            option.success(data, tempFilePaths)
          }
        })

      }
    })
  },
  //获取openid
  getOpenid1: function (fn = null) {
        wx.login({
          success: function (res) {
            if (res.code) {
              var user = app.globalData.wxuserInfo
              try{
                var ava = user.avatarUrl
                var city= user.city
                var country= user.country
                var gender= user.gender
                var language= user.language
                var nickName=user.nickName
                var province= user.province
              }catch(e){

              }
              wx.request({
                url: app.globalData.report + "/wx/auth/login_by_wx",
                header: {
                  "content-type": "application/json;charset=UTF-8",
                  "Authorization": "Basic c2FiZXI6c2FiZXJfc2VjcmV0"
                },
                data: {
                  "code": res.code,
                  "tenantId":"642283",
                  "userInfo": {
                    "avatarUrl": ava,
                    "city": city,
                    "country": country,
                    "gender": gender,
                    "language": language,
                    "nickName": nickName,
                    "province": province
                  }
                },
                method: 'POST',
                success: function (res) {
                  fn(res.data)
                },
                fail: function () {
                  wx.showToast({
                    title: '登录失败',
                    duration: 3000
                  });
                }
              });
            } else {
              wx.showToast({
                title: '获取用户登录态失败！',
                duration: 3000
              });
            }
          },
          fail: function () {
            wx.showToast({
              title: '授权失败',
              duration: 3000
            });
          }
        });
  },
  //返回上一页
  backPage: function(ref) {
    var pages = getCurrentPages();   //当前页面
    var prevPage = pages[pages.length - 2];   //上一页面
    prevPage.setData({
      refresh: ref
    });
    if (ref) {
      wx.navigateBack({
        //返回
        delta: 1
      })
    }
  },
  //返回刷新
  backRefresh: function(fn) {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    console.log(currPage.data)
    if (currPage.data.refresh) {
      fn()
    }
  },
  nowDate: function() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    return (year + "-" + month + "-" + day);
  },
  nowTime: function() {
    var myDate = new Date();
    var h = myDate.getHours(); //获取系统时，
    var m = myDate.getMinutes(); //分
    var s = myDate.getSeconds(); //秒
    if (h < 10) {
      h = "0" + h;
    }
    if (m < 10) {
      m = "0" + m;
    }
    if (s < 10) {
      s = "0" + s;
    }
    return (h + ":" + m + ":" + s);
  }

}
module.exports.comm = Model;